package com.kmproject.haifu.mvp.ui.activity;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.StatisticsBean;
import com.kmproject.haifu.mvp.ui.adapter.FragmentTitlesAdapter;
import com.kmproject.haifu.mvp.ui.fragment.HomeNewsFragment;
import com.kmproject.haifu.mvp.ui.fragment.MyRentObtainedFragment;
import com.kmproject.haifu.mvp.ui.fragment.MySecondHandObtainedFragment;
import com.kmproject.haifu.mvp.ui.widget.NoScrollViewPager;
import com.kmproject.haifu.mvp.ui.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyObtainedTabHostActivity extends FragmentActivity {
    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.mi_home_news_tab)
    MagicIndicator miHomeNewsTab;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;
    private TabHost tabHost;
    int width = 60;
    int height = 60;
    Field mBottomLeftStrip;
    Field mBottomRightStrip;
    private List<String> titles;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_obtained_tab_host);
        ButterKnife.bind(this);
        toolbar.setBackgroundColor(ArmsUtils.getColor(this,R.color.color_EDEDED));
//        toolbarTitle.setText("已下架");
        StatisticsBean statisticsBean=(StatisticsBean)getIntent().getSerializableExtra("StatisticsBean");
        titles=new ArrayList<>();
        titles.add("租房("+statisticsBean.getUnderHouseRentCount()+")");
        titles.add("二手物品("+statisticsBean.getUnderSecondHandCount()+")");
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(MyRentObtainedFragment.newInstance());
        fragments.add(MySecondHandObtainedFragment.newInstance());

        viewpager.setAdapter(new FragmentTitlesAdapter(getSupportFragmentManager(), this, titles, fragments));
        viewpager.setCurrentItem(0);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles.get(index));

//                simplePagerTitleView.setWidth(ScreenUtils.getScreenWidth() / 6);
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_191919));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_191919));
//                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//                layoutParams.gravity = Gravity.END;
////                layoutParams.setMargins(0, 0, 0, 0);//4个参数按顺序分别是左上右下
//                simplePagerTitleView.setLayoutParams(layoutParams);
                simplePagerTitleView.setOnClickListener(v -> {
                    miHomeNewsTab.onPageSelected(index);
                    commonNavigator.getAdapter().notifyDataSetChanged();
                    viewpager.setCurrentItem(index);
                });

                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setLineHeight(8.0f);
                linePagerIndicator.setColors(ArmsUtils.getColor(MyObtainedTabHostActivity.this, R.color.color_F9D35A));

                return linePagerIndicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }

        });

        miHomeNewsTab.setNavigator(commonNavigator);
        ViewPagerHelper.bind(miHomeNewsTab, viewpager);
    }

    public void setMyStyle(TabHost tabs) {
        //        tabHost = getTabHost();
//        addTab("act1", "租房", MyStoreLookObtainedActivity.class);//添加
//        addTab("act2", "二手物品", SecondhandObtainedActivity.class);
//        setMyStyle(tabHost);
        final TabWidget tabWidget = tabs.getTabWidget();
        for (int i = 0; i < tabWidget.getChildCount(); i++) {
            /**
             * 设置高度、宽度，不过宽度由于设置为fill_parent，在此对它没效果
             */
            tabWidget.getChildAt(i).getLayoutParams().height = height;
            tabWidget.getChildAt(i).getLayoutParams().width = width;
            /**
             * 设置tab中标题文字的颜色，不然默认为黑色
             */
            final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);

            tv.setTextColor(ArmsUtils.getColor(this, R.color.color_191919));
            tv.setTextSize(16);
            /**
             * 此方法是为了去掉系统默认的色白的底角
             *
             * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
             * 都是私有变量，但是我们可以通过反射来获取
             *
             * 由于还不知道Android 2.2的接口是怎么样的，现在先加个判断好一些
             */

            if (Float.valueOf(Build.VERSION.RELEASE.substring(0, 3)) <= 2.1) {
                try {
                    mBottomLeftStrip = tabWidget.getClass().getDeclaredField("mBottomLeftStrip");
                    mBottomRightStrip = tabWidget.getClass().getDeclaredField("mBottomRightStrip");
                    if (!mBottomLeftStrip.isAccessible()) {
                        mBottomLeftStrip.setAccessible(true);
                    }
                    if (!mBottomRightStrip.isAccessible()) {
                        mBottomRightStrip.setAccessible(true);
                    }
                    mBottomLeftStrip.set(tabWidget, getResources().getColor(R.color.color_F9D35A));
                    mBottomRightStrip.set(tabWidget, getResources().getColor(R.color.color_F9D35A));

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                /**
                 * 不做任何处理
                 */
            }
            View vvv = tabWidget.getChildAt(i);
            vvv.setBackgroundColor(ArmsUtils.getColor(this, R.color.white));
            if (tabs.getCurrentTab() == i) {
//                vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.navbgfocus));
            } else {
//                vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubg));
            }

        }
        /**
         * 当点击tab选项卡的时候，更改当前的背景
         */
        tabs.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                // TODO Auto-generated method stub
                for (int i = 0; i < tabWidget.getChildCount(); i++) {
                    View vvv = tabWidget.getChildAt(i);
                    if (tabs.getCurrentTab() == i) {
                        if (vvv instanceof TextView) {
                            ((TextView) vvv).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

                        }
//                        vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.menufocus));
                    } else {
                        if (vvv instanceof TextView) {
                            ((TextView) vvv).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

                        }
//                        vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubg));
                    }
                }
            }
        });

    }

    /**
     * 添加Activity标签
     *
     * @param tag   标识
     * @param title 标签标题
     * @param clazz 激活的界面
     */
    private void addTab(String tag, String title, Class clazz) {
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
        tabSpec.setIndicator(title);

        Intent intent = new Intent(getApplicationContext(), clazz);
        intent.putExtra("HousePayWayBean", (Serializable) ((List<HousePayWayBean>) getIntent().getSerializableExtra("HousePayWayBean")));
        intent.putExtra("type", "userObtained");
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);
    }

}
